﻿//模块名称:系统页面基类
//功能描述:封装列表页的常用方法
//创建人:  Lam
//创建时间:2010年4月18日16:59:19
//修改描述:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using webnew.Common;
using System.Reflection;
using webnew.DBUtility;
using webnew.Model;
using webnew;
using WebPageBase;

namespace webnew
{
    /// <summary>
    /// 列表页面
    /// </summary>
    public class ListBase : PageBase
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            //检测当前用户是否有权限打开当前页，否：跳转到错误页，是：进入当前页面
            if (!ValidPermissions.ValidOperatePage(UserInfor.Id, this.GetType().BaseType.Name)) Response.Redirect("~/ErroPage.aspx?erro=0");
        }

        protected override void OnLoad(EventArgs e)
        {
            if (this.Page != null)
            {
                CreateOperateBtn();
                //父类onload，判断用户身份是否有效
                base.OnLoad(e);

            }

        }

        #region 动态生成权限操作按钮(包含新建按钮，绑定事件，事件方法)
        /// <summary>
        /// 动态生成权限操作按钮
        /// </summary>
        protected void CreateOperateBtn()
        {
            //加载列表页JS
            ClientScript.RegisterClientScriptInclude(this.GetType(), "Grd", "../Js/dgrcheck.js");
            //此处生成系统自定义权限按钮（添加(Add)，修改(Modi)，删除(Del)，查看(View)，导出(Excel) 为系统默认按钮事件）
            //其他自定义按钮需要为该按钮引用事件
            try
            {
                Panel pnlBtns = (Panel)this.Master.FindControl("pnlBtn");
                //this.GetType().BaseType.Name 获取当前打开的页面名
                List<webnew.Model.Sys_NavigationInfo> lst = new webnew.BLL.Sys_Navigation().GetUserControl(UserInfor.Id, this.GetType().BaseType.Name);
                foreach (webnew.Model.Sys_NavigationInfo model in lst)
                {
                    Button btnTest = new Button();
                    btnTest.ID = model.ControlID;
                    btnTest.Text = model.NaviName;
                    btnTest.CssClass = "x-panel-header";

                    btnTest.CommandName = model.FunctionName;
                    //根据按钮事件名 加载各自需要的JS脚本
                    switch (model.FunctionName)   //加载 修改 ,删除按钮的选择脚本
                    {
                        case "Modi":

                        case "Function1":                //通用按钮1
                        case "Function2":                //通用按钮2
                        case "Function3":                //通用按钮3

                        case "Transposition":                //IC卡系统-发油列表-换位按钮
                        case "ReceiveOil":                   //IC卡系统-发油列表-收油按钮
                        case "UpLoad":                       //IC卡系统-发油列表-上传按钮
                            btnTest.Attributes.Add("onclick", "return modi()");
                            break;
                        case "Del":

                            btnTest.Attributes.Add("onclick", "return del()");
                            break;
                        case "Receive":
                            btnTest.Attributes.Add("onclick", "return check()");
                            break;
                        case "CTCheck":                        //IC卡系统-出库记录三方确认-车队确认按钮

                            btnTest.Attributes.Add("onclick", "return msgbox('')");
                            break;
                        case "CPCheck":                        //IC卡系统-出库记录三方确认-省公司确认按钮

                            btnTest.Attributes.Add("onclick", "return msgbox('')");
                            break;

                        default:
                            break;
                    }
                    btnTest.Command += Button_Command;
                    pnlBtns.Controls.Add(btnTest);
                }

                //系统默认自带的查看(View)，导出(Excel)按钮，不需在导航里添加
                //
                //查看按钮
                Button btnView = new Button();
                btnView.ID = "btnView";
                btnView.Text = "查看";
                btnView.CssClass = "x-panel-header";
                btnView.CommandName = "View";
                btnView.Command += Button_Command;
                btnView.Attributes.Add("onclick", "return modi()"); //加载查看按钮的选择脚本
                pnlBtns.Controls.Add(btnView);


                //导出Excel按钮
                Button btnExcel = new Button();
                btnExcel.ID = "btnExcel";
                btnExcel.Text = "导出Excel";
                btnExcel.CssClass = "x-panel-header";
                btnExcel.CommandName = "Excel";
                btnExcel.Command += Button_Command;
                btnExcel.Width = 75;
                pnlBtns.Controls.Add(btnExcel);


            }
            catch (Exception)
            {
                // Server.Transfer("~/ErroPage.aspx?erro=2");
                Response.Redirect("~/ErroPage.aspx?erro=2");
            }
        }

        /////////////////////自定义按钮调用方法////////////////////////////////////////////
        //在导航添加的按钮需在这里添加调用的虚构方法,可在具体的页面重写该按钮方法
        protected void Button_Command(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add": //添加按钮
                    _Add();
                    break;
                case "Modi"://修改按钮

                    _Modi();
                    break;

                case "Del"://删除按钮

                    _Del();
                    break;

                case "Function1"://通用按钮1

                    _Function1();
                    break;

                case "Function2"://通用按钮2

                    _Function2();
                    break;

                case "Function3"://通用按钮3

                    _Function3();
                    break;

                case "Function4"://通用按钮4

                    _Function4();
                    break;

                case "Function5"://通用按钮5

                    _Function5();
                    break;

                case "CTCheck":  //IC卡系统-出库记录三方确认-车队确认按钮
                    _CTCheck();
                    break;

                case "CPCheck":  //IC卡系统-出库记录三方确认-省公司确认按钮

                    _CPCheck();
                    break;
                case "View"://查看按钮

                    _View();
                    break;
                case "Excel"://导出Excel按钮
                    _Excel();
                    break;

                case "Issue"://调度系统-作业安排-下发按钮
                    _Issue();
                    break;

                case "Receive"://调度系统-质检通知单-接收按钮
                    _Receive();
                    break;
                case "Transposition":
                    _Transposition();           //IC卡系统-发油列表-换位按钮
                    break;
                case "ReceiveOil":
                    _ReceiveOil();             //IC卡系统-发油列表-收油按钮
                    break;
                case "UpLoad":
                    _UpLoad();                 //IC卡系统-发油列表-上传按钮
                    break;
                case "UnLock":                 //计量单 解锁按钮
                    _UnLock();
                    break;
                case "Lock":                 //计量单 解锁按钮
                    _Lock();
                    break;
                default:
                    break;
            }
        }

        #region 按钮事件虚构函数，可在具体的页面重写方法
        /// <summary>
        ///  添加（可重写）
        /// </summary>
        protected virtual void _Add()
        {
            Response.Redirect(_EditSrc + (_SrcPrams != null ? "?" + _SrcPrams : ""), true);
        }
        /// <summary>
        ///  修改（可重写）
        /// </summary>
        protected virtual void _Modi()
        {
            string SelId = GetSelecetedIDs(_Grdview);
            if (SelId.IndexOf(",") > -1 || SelId == string.Empty) { Jscript.Alert(this.Page, "请选择一项需要修改的记录!"); return; }
            Response.Redirect(_EditSrc + @"?" + (_SrcPrams != null ? _SrcPrams + @"&" : "") + "id=" + GetSelecetedIDs(_Grdview).Replace("'", ""), true);
        }
        /// <summary>
        ///  删除（可重写）
        /// </summary>
        protected virtual void _Del()
        {
            string sqlStr = string.Empty;
            if (_DelType == DeleteType.FalseDel)
            {
                sqlStr = "UPDATE " + _ListTbName + " SET isdel=1 WHERE " + _Grdview.DataKeyNames[0].ToString();
                sqlStr += " in(" + GetSelecetedIDs(_Grdview) + ")";
            }
            else
            {
                sqlStr = "DELETE FROM " + _ListTbName + " WHERE " + _Grdview.DataKeyNames[0] + " in(" + GetSelecetedIDs(_Grdview) + ")";
            }
            webnew.DBUtility.SqlHelper DB = null;
            if (_ConnectionString == string.Empty || _ConnectionString == null) DB = new webnew.DBUtility.SqlHelper();
            else DB = new webnew.DBUtility.SqlHelper(_ConnectionString);
            if (DB.ExecuteNonQuery(System.Data.CommandType.Text, sqlStr, null) > 0)
                ShowMsg = "删除成功!";//Jscript.Alert(this.Page, "删除成功!");
            else
                ShowMsg = "删除失败!";// Jscript.Alert(this.Page, "删除失败!");
        }
        /// <summary>
        ///  查看（可重写）
        /// </summary>
        protected virtual void _View()
        {
            string SelId = GetSelecetedIDs(_Grdview);
            if (SelId.IndexOf(",") > -1 || SelId == string.Empty) { Jscript.Alert(this.Page, "请选择一项需要查看的记录!"); return; }
            Response.Redirect(_EditSrc + @"?" + (_SrcPrams != null ? _SrcPrams + @"&" : "") + @"read=1&id=" + GetSelecetedIDs(_Grdview).Replace("'", ""), true);
        }
        /// <summary>
        ///  导出（可重写）
        /// </summary>
        protected virtual void _Excel() { }
        /// <summary>
        /// 下发（调度系统-作业安排-下发按钮）（可重写）
        /// </summary>
        protected virtual void _Issue() { }


        /// <summary>
        /// 接收（调度系统-质检通知单-接收按钮）（可重写）
        /// </summary>
        protected virtual void _Receive() { }

        /// <summary>
        /// 通用按钮1
        /// </summary>
        protected virtual void _Function1() { }

        /// <summary>
        /// 通用按钮2
        /// </summary>
        protected virtual void _Function2() { }

        /// <summary>
        /// 通用按钮3
        /// </summary>
        protected virtual void _Function3() { }

        /// <summary>
        /// 通用按钮4
        /// </summary>
        protected virtual void _Function4() { }

        /// <summary>
        /// 通用按钮5
        /// </summary>
        protected virtual void _Function5() { }

        /// <summary>
        /// 换位（IC卡系统）（可重写）
        /// </summary>
        protected virtual void _Transposition() { }

        /// <summary>
        /// 车队确认（IC卡系统）（可重写）
        /// </summary>
        protected virtual void _CTCheck() { }

        /// <summary>
        /// 省公司确认（IC卡系统）（可重写）
        /// </summary>
        protected virtual void _CPCheck() { }


        /// <summary>
        /// 收油（IC卡系统）（可重写）
        /// </summary>
        protected virtual void _ReceiveOil() { }

        /// <summary>
        /// 上传（IC卡系统）（可重写）
        /// </summary>
        protected virtual void _UpLoad() { }
        /// <summary>
        /// 解锁（计量单系统）（可重写）
        /// </summary>
        protected virtual void _UnLock() { }
        /// <summary>
        /// 锁定（计量单系统）（可重写）
        /// </summary>
        protected virtual void _Lock() { }



        #endregion

        #endregion

        //private void getClassAllMeth()
        //{
        //    Type t = typeof(webnew.PageBase);
        //    //获取所有方法 
        //    System.Reflection.MethodInfo[] methods = t.GetMethods();
        //    string strmethName = "";
        //    foreach (System.Reflection.MethodInfo method in methods)
        //    {
        //        strmethName += method.Name + System.Environment.NewLine;
        //    }
        //    ShowMsg = strmethName;
        //}

        #region 默认gridview方法
        /// <summary>
        /// 获取当前选中记录的ID
        /// </summary>
        protected string GetSelecetedIDs(GridView grd)
        {
            string RowsId = string.Empty;
            foreach (GridViewRow r in grd.Rows)
            {
                if (((CheckBox)r.FindControl("chkitem")).Checked)
                {
                    if (RowsId == string.Empty) RowsId += "'" + grd.DataKeys[r.RowIndex].Value + "'";
                    else RowsId += "," + "'" + grd.DataKeys[r.RowIndex].Value + "'";
                }
            }
            return RowsId;
        }


        ///编写人 
        ///日期   2010-05-31
        ///作用   系统日志 删除所需
        /// <summary>
        /// 获得要删除的表中的某个或者多个字段
        /// </summary>
        /// <param name="selFildName">字段</param>
        /// <param name="delTableName">表名</param>
        /// <returns></returns>
        protected string GetNames(string selFildName, string delTableName)
        {
            string names = "";
            string strSql = "";
            string[] rowsID = GetSelecetedIDs(_Grdview).Split(',');
            for (int i = 0; i < rowsID.Length; i++)
            {
                strSql = "select " + selFildName + " from " + delTableName + " where " + _Grdview.DataKeyNames[0] + " = " + rowsID[i] + " ";
                if (names == "")
                {
                    names = webnew.Common.MyConvert.toString(new webnew.DBUtility.SqlHelper().ExecuteScalar(strSql));
                }
                else names += "," + webnew.Common.MyConvert.toString(new webnew.DBUtility.SqlHelper().ExecuteScalar(strSql));

            }

            return names;
        }

        #endregion


        /// <summary>
        /// 删除类别枚举
        /// </summary>
        protected enum DeleteType
        {
            TrueDel,  //物理删除
            FalseDel //逻辑删除
        }


        #region "列表页属性"
        /// <summary>
        /// 执行删除语句的数据库表名
        /// </summary>
        protected string _ListTbName { get; set; }

        /// <summary>
        /// 编辑页路径
        /// </summary>
        protected string _EditSrc { get; set; }


        /// <summary>
        /// 传递参数 如quit=2,请勿与系统框架定义的id,read参数名冲突
        /// </summary>
        protected string _SrcPrams { get; set; }


        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string _ConnectionString { get; set; }


        /// <summary>
        /// 主列表控件（显示数据）
        /// </summary>
        protected GridView _Grdview { get; set; }

        /// <summary>
        /// 设置并显示错误的提示信息
        /// </summary>
        public string ShowMsg
        {
            set
            {
                if (value == "") ((Literal)this.Master.FindControl("ltrlMsg")).Text = value;
                else ((Literal)this.Master.FindControl("ltrlMsg")).Text = " >> " + value;
            }
        }

        /// <summary>
        /// 设置页面宽度
        /// </summary>
        public string SetPageWidth
        {
            set { ((System.Web.UI.HtmlControls.HtmlGenericControl)this.Master.FindControl("mainBody")).Style["width"] = value; }
        }



        /// <summary>
        /// 设置导出Excel按钮是否可见,默认为可见
        /// </summary>
        protected bool btnExcelVisble
        {
            set { this.Master.FindControl("btnExcel").Visible = value; }
        }


        /// <summary>
        /// 设置导出查看按钮是否可见,默认为可见
        /// </summary>
        protected bool btnViewVisble
        {
            set { this.Master.FindControl("btnView").Visible = value; }
        }


        /// <summary>
        /// 删除类别,TrueDel:物理删除;FalseDel:逻辑删除 isdel=1
        /// </summary>
        protected DeleteType _DelType { get; set; }
        #endregion
    }
}
